package com.lianzhihui.romeex.ui.account;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.MyToast;
import com.lianzhihui.romeex.widget.SafetyKeyWordsListener;
import com.lianzhihui.romeex.widget.keybord.SafetyKeyBoardView;

import butterknife.BindView;
import butterknife.OnTextChanged;

/**
 * ProjectName:    NBN
 * ClassName:      ResetSaftyCodeActivity
 * Description:    设置、重置安全码
 * Author:         echo
 * CreateDate:     2019/11/22 13:29
 * UpdateUser:     更新者
 * UpdateDate:     2019/11/22 13:29
 * UpdateRemark:   更新说明
 * Version:        1.0
 */
public class SetSaftyCodeActivity extends BaseActivity implements SafetyKeyWordsListener, com.lianzhihui.romeex.widget.keybord.SafetyKeyWordsListener {

    @BindView(R.id.tv_describe)
    TextView tv_describe;
    @BindView(R.id.safetyKeyBoardView)
    SafetyKeyBoardView safetyKeyBoardView;
    @BindView(R.id.et_code_one)
    TextView et_code_one;
    @BindView(R.id.et_code_two)
    TextView et_code_two;
    @BindView(R.id.et_code_three)
    TextView et_code_three;
    @BindView(R.id.et_code_four)
    TextView et_code_four;
    @BindView(R.id.et_code_five)
    TextView et_code_five;
    @BindView(R.id.et_code_six)
    TextView et_code_six;
    @BindView(R.id.view_label_one)
    View view_label_one;
    @BindView(R.id.view_label_two)
    View view_label_two;
    @BindView(R.id.view_label_three)
    View view_label_three;
    @BindView(R.id.view_label_four)
    View view_label_four;
    @BindView(R.id.view_label_five)
    View view_label_five;
    @BindView(R.id.view_label_six)
    View view_label_six;
    @BindView(R.id.tv_code_error)
    TextView tv_code_error;
    private int stepNum = 1;
    private String firstCode;
    private String address;
    private long delayMillis = 40;
    private boolean isManage;
    private WalletInfo walletInfoCache;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_safty_code;
    }

    @Override
    protected void initView() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setTitle(getResources().getString(R.string.toolbar_title_safty_code));
        isManage = getIntent().getBooleanExtra("isManage", false);
        walletInfoCache = (WalletInfo) getIntent().getSerializableExtra("walletInfo");
        safetyKeyBoardView.setOnKeyWordsListener(this);
        safetyKeyBoardView.showPasswodPanel(false);
        cleanInput();
    }

    @Override
    protected void initData() {

    }

    @OnTextChanged(R.id.et_code_one)
    public void onTextChangeOne(CharSequence text) {
        if (et_code_one.getText().length() == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    et_code_one.setFocusable(false);
                    et_code_two.setFocusable(true);
                }
            }, delayMillis);
        }
    }

    @OnTextChanged(R.id.et_code_two)
    public void onTextChangeTwo(CharSequence text) {
        if (et_code_two.getText().length() == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    et_code_two.setFocusable(false);
                    et_code_three.setFocusable(true);
                }
            }, delayMillis);
        }
    }

    @OnTextChanged(R.id.et_code_three)
    public void onTextChangeThree(CharSequence text) {
        if (et_code_three.getText().length() == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    et_code_three.setFocusable(false);
                    et_code_four.setFocusable(true);
                }
            }, delayMillis);
        }
    }

    @OnTextChanged(R.id.et_code_four)
    public void onTextChangeFour(CharSequence text) {
        if (et_code_four.getText().length() == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    et_code_four.setFocusable(false);
                    et_code_five.setFocusable(true);
                }
            }, delayMillis);
        }
    }

    @OnTextChanged(R.id.et_code_five)
    public void onTextChangeFive(CharSequence text) {
        if (et_code_five.getText().length() == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    et_code_five.setFocusable(false);
                    et_code_six.setFocusable(true);
                }
            }, delayMillis);
        }
    }

    @OnTextChanged(R.id.et_code_six)
    public void onTextChangeSix(CharSequence text) {
        if (et_code_six.getText().length() == 1) {
            String code = et_code_one.getText().toString() +
                    et_code_two.getText().toString() +
                    et_code_three.getText().toString() +
                    et_code_four.getText().toString() +
                    et_code_five.getText().toString() +
                    et_code_six.getText().toString();
            if (stepNum == 1) {
                firstCode = code;
                stepNum = 2;
                tv_describe.setText(getResources().getString(R.string.input_safety_code_again));
                cleanInput();
            } else {
                verifyCode(code);
            }
        }
    }

    private void verifyCode(String codeAgain) {
        if (firstCode.equals(codeAgain)) {
            tv_code_error.setVisibility(View.GONE);
            walletInfoCache.setSaftyCodeSample(codeAgain);
            Intent intent = new Intent(this, MnemonicWordActivity.class);
            intent.putExtra("isManage", isManage);
            intent.putExtra("walletInfo", walletInfoCache);
            startActivity(intent);
            finish();
        } else {
            showError(true);
            tv_code_error.setVisibility(View.VISIBLE);
            MyToast.showToast(getString(R.string.nbn_security_code_not));
            cleanInput();
        }
    }

    public void showError(boolean show) {
        if (show) {
            view_label_one.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
            view_label_two.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
            view_label_three.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
            view_label_four.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
            view_label_five.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
            view_label_six.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
            tv_code_error.setVisibility(View.VISIBLE);
        } else {
            view_label_one.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
            view_label_two.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
            view_label_three.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
            view_label_four.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
            view_label_five.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
            tv_code_error.setVisibility(View.INVISIBLE);
        }
    }

    private void cleanInput() {
        et_code_one.setText("");
        et_code_one.setFocusable(false);
        et_code_two.setText("");
        et_code_two.setFocusable(false);
        et_code_three.setText("");
        et_code_three.setFocusable(false);
        et_code_four.setText("");
        et_code_four.setFocusable(false);
        et_code_five.setText("");
        et_code_five.setFocusable(false);
        et_code_six.setText("");
        et_code_six.setFocusable(false);
        et_code_one.setFocusable(true);
        view_label_one.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_two.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_three.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_four.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_five.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_six.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
    }

    private void cleanFocsable() {
        et_code_one.setFocusable(false);
        et_code_two.setFocusable(false);
        et_code_three.setFocusable(false);
        et_code_four.setFocusable(false);
        et_code_five.setFocusable(false);
        et_code_six.setFocusable(false);
        view_label_one.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_two.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_three.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_four.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_five.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
        view_label_six.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
    }

    @Override
    public void onResultWord(String word) {
        if (et_code_one.hasFocusable()) {
            et_code_one.setText(word);
            view_label_one.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
        }
        if (et_code_two.hasFocusable()) {
            et_code_two.setText(word);
            view_label_two.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
        }
        if (et_code_three.hasFocusable()) {
            et_code_three.setText(word);
            view_label_three.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
        }
        if (et_code_four.hasFocusable()) {
            et_code_four.setText(word);
            view_label_four.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
        }
        if (et_code_five.hasFocusable()) {
            et_code_five.setText(word);
            view_label_five.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
        }
        if (et_code_six.hasFocusable()) {
            et_code_six.setText(word);
            if (stepNum == 1) {
                view_label_six.setBackgroundColor(ContextCompat.getColor(this, R.color.colorText));
            }
        }
    }

    @Override
    public void onDeleteWord() {
        if (et_code_one.hasFocusable()) {
            et_code_one.setText("");
            view_label_one.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
            cleanFocsable();
            et_code_one.setFocusable(true);
        }
        if (et_code_two.hasFocusable()) {
            et_code_two.setText("");
            view_label_two.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
            cleanFocsable();
            et_code_one.setFocusable(true);
        }
        if (et_code_three.hasFocusable()) {
            et_code_three.setText("");
            view_label_three.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
            cleanFocsable();
            et_code_two.setFocusable(true);
        }
        if (et_code_four.hasFocusable()) {
            et_code_four.setText("");
            view_label_four.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
            cleanFocsable();
            et_code_three.setFocusable(true);
        }
        if (et_code_five.hasFocusable()) {
            et_code_five.setText("");
            view_label_five.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
            cleanFocsable();
            et_code_four.setFocusable(true);
        }
        if (et_code_six.hasFocusable()) {
            et_code_six.setText("");
            view_label_six.setBackgroundColor(ContextCompat.getColor(this, R.color.gray_password_label));
            cleanFocsable();
            et_code_five.setFocusable(true);
        }
    }

}
